<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Member;
use App\Models\Member as ModelsMember;
use App\Models\User;
use App\Models\UserType;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Redis;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {

            // $grid->scrollbarX();
            $grid->withBorder();
            $grid->addTableClass(['table-text-center']);

            // @permissions
            if (!Admin::user()->can('user-info-create')) {
                $grid->disableCreateButton();
                // $grid->disableQuickCreateButton();
            }

            // $grid->disableCreateButton();
            $grid->column('id', '用户ID')->sortable();
            $grid->column('username')->copyable('复制');
            // $grid->column('wx.nickname', '微信昵称')->copyable('复制');
            // $grid->avatar('头像')->customImage();
            $grid->column('phone', '手机号码');

            /**
             * 按钮控制.
             */
            // @permissions
            if (!Admin::user()->can('user-info-create')) {
                $grid->disableCreateButton();
                $grid->disableQuickCreateButton();
            }
            // @permissions
            if (!Admin::user()->can('user-info-update')) {
                $grid->disableEditButton();
                $grid->disableQuickEditButton();
            }
            // @permissions
            if (!Admin::user()->can('user-info-delete')) {
                $grid->disableDeleteButton();
            }
            // @permissions
            if (!Admin::user()->can('user-info-view')) {
                $grid->disableViewButton();
            }


            $grid->model()->orderBy('id', 'desc');
            $grid->filter(function (Grid\Filter $filter) {
                // $filter->equal('userid');
                $filter->like('username');
                $filter->like('phone');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $model = new User();
        return Show::make($id, $model, function (Show $show) {
            $show->field('id');
            $show->field('username');
            // $show->field('password');

            $show->field('realname', '真实姓名');
            // $show->avatar('头像')->image();
            $show->field('email');
            $show->field('phone', '手机号');
            // $show->field('sex');
            $show->field('last_login_time', '最后登录时间')->as(function ($time) {
                return date('Y-m-d H:i:s', $time);
            });
            // $show->field('reg_ip');
            // $show->field('add_time')->as(function($time){
            //     return date('Y-m-d H:i:s',$time);
            // });

            $show->status('状态')->using(['1' => '显示', '2' => '禁用']);
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->display('id');
            $form->text('username')->required();
            $form->password('password')->required();

            $form->text('realname', '真实姓名')->required();
            // $form->image('avatar', '头像')->move('member/avatar/' . date('Y/m'));
            $form->text('email')->required();
            $form->text('phone', '手机号码')->required();

            // if ($form->isCreating()) {
            //     $form->saving(function (Form $form) {
            //         // Redis::set('json:' . $form->getKey(), []);
            //         // dd(json_encode($form->input('json')));
            //         $form->password = bcrypt($form->input('password'));
            //         // $form->json = json_encode($form->input('json'));
            //     });
            // }

            if ($form->isEditing()) {
                $form->saving(function (Form $form) {
                    if (is_numeric($form->input('password'))) {
                        $form->password = bcrypt($form->input('password'));
                    } else {
                        $form->password = $form->input('password');
                    }
                });
            }
            // dd($form->password);
            $form->radio('status', '状态')->options([1 => '正常', 2 => '禁用'])->default(1);
        });
    }
}
